<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/9/21 10:53
 */

namespace module\document\controller;

use module\document\enum\DocumentTypeEnum;
use module\document\logic\DocumentLogic;
use mof\annotation\Inject;
use mof\ApiController;
use mof\ApiResponse;
use mof\Searcher;
use think\db\exception\DataNotFoundException;
use think\response\Json;

class Index extends ApiController
{
    #[Inject]
    protected DocumentLogic $logic;

    public function page($name): Json
    {
        return ApiResponse::success($this->logic->page($name));
    }

    /**
     * 列表
     * @param string $type 类型 DocumentTypeEnum
     * @return Json
     */
    public function list(string $type): Json
    {
        if (!$enum = DocumentTypeEnum::tryFrom($type)) {
            return ApiResponse::error('参数错误');
        }

        $searcher = (new Searcher())->params(['type' => $enum->value, 'status' => 1]);
        $paginate = $this->logic->paginate($searcher, true);
        $paginate->count() && $paginate->getCollection()->scene('list');

        return ApiResponse::success($paginate);
    }

    /**
     * @param $id
     * @return Json
     * @throws DataNotFoundException
     */
    public function read($id): Json
    {
        $detail = $this->logic->withAccess(['status' => 1])->read($id);
        return ApiResponse::success($detail->scene('detail'));
    }
}